package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Slf4j
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    //查询当前时间正在参与秒杀的商品信息
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurSeckillSkus() {
        log.info("currentSeckillSkus执行。。。。。。");
        List<SeckillSkuRedisTo> list = seckillService.getCurSeckillSkus();
        return R.ok().setData(list);
    }

    //查询skuId商品的秒杀预告信息
    @ResponseBody
    @GetMapping("sku/skuInfo/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    @GetMapping("/kill")
    public String  kill(@RequestParam("killId") String killId, // session_skuID
                  @RequestParam("key") String key,
                  @RequestParam("num") Integer num, Model model){
        String orderSn = seckillService.kill(killId, key, num);
        // 1.判断是否登录
        model.addAttribute("orderSn", orderSn);
        return "success";
    }

}
